package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.dao.impl.UserDaoImpl;
import org.entity.UserData;
public class SaveUserData extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8"); 
		
		String name=request.getParameter("name");
		String school=request.getParameter("school");
		String address=request.getParameter("address");
		String beizhu=request.getParameter("beizhu");
		if(name.equals("")){
			name="未填写";
		}
		if(school.equals("")){
			school="未填写";
		}
		if(address.equals("")){
			address="未填写";
		}
		if(beizhu.equals("")){
			beizhu="未填写";
		}
		
		
		UserData userData=new UserData();
		userData.setAddress(address);
		userData.setName(name);
		userData.setBeizhu(beizhu);
		userData.setSchool(school);
		HttpSession session = request.getSession();
		session.setAttribute("userData",userData);
		session.setAttribute("saveDataFlag","true");
		userData.setUsername((String) session.getAttribute("username"));
		UserDaoImpl ud=new UserDaoImpl();
		if(ud.queryUserExits(userData)){
			ud.alterUserData(userData);
		}else{
			ud.addUserData(userData);
		}
		
		request.getRequestDispatcher("personal.jsp").forward(request, response);;
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
